//
//  WZYPresentationManager.swift
//  SwiftWB
//
//  Created by 吴志勇 on 2019/6/20.
//  Copyright © 2019 wzy. All rights reserved.
//

import UIKit

let WZYPresentationManagerDidPresented = "WZYPresentationManagerDidPresented"
let WZYPresentationManagerDidDismissed = "WZYPresentationManagerDidDismissed"



class WZYPresentationManager: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning
{
    //记录当前是否是展现
    private var isPresent = false
    //保存菜单尺寸
    var presentFrame = CGRect.zero
    
    
    //MARK: -UIViewControllerTransitioningDelegate
    //该方法用于返回一个负责转场动画的对象
    //可以在该对象中控制弹出视图的尺寸等
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let pc = WZYPresentationController(presentedViewController: presented, presenting: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    
    //该方法用于返回一个负责转场如何出现的对象
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = true
        //发送一个通知，告诉调用者状态发生了改变
        NotificationCenter.default.post(name: NSNotification.Name(WZYPresentationManagerDidPresented), object: nil)
        return self
    }
    
     //该方法用于返回一个负责转场如何消失的对象
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        //发送一个通知，告诉调用者状态发生了改变
        NotificationCenter.default.post(name: NSNotification.Name(WZYPresentationManagerDidDismissed), object: nil)
        return self
    }
    
    //MARK: -UIViewControllerAnimatedTransitioning
    //告诉系统展现和消失的动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
        return 0.5
    }
    
    /*专门用于管理modal如何展现和消失的，无论是展现还是消失都会调用该方法
     注意点: 只要我们实现了这个代理方法，那么系统就不会再有默认的动画了
     也就是说默认的modal从下至上的移动系统不帮我们添加，所有的e动画操作都需要我们自己实现，包括需要展现的视图也需要我们自己添加到容器视图上(containerView)*/
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
        //获取需要弹出的视图
        
        if isPresent{
            willPresentationController(transitionContext: transitionContext)
        }else{
            willDismissedController(transitionContext: transitionContext)
        }
    }
    
    private func willPresentationController(transitionContext: UIViewControllerContextTransitioning){
        guard let toView =  transitionContext.view(forKey: .to) else {
            return
        }
        
        transitionContext.containerView.addSubview(toView)
        
        toView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        //设置锚点
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext)
            , animations: {
                toView.transform = CGAffineTransform.identity
                
        }) { (_) in
            transitionContext.completeTransition(true)
        }
        
    }
    
    //
    private func willDismissedController(transitionContext: UIViewControllerContextTransitioning){
        guard let fromVC = transitionContext.view(forKey: .from) else{
            return
        }
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            fromVC.transform = CGAffineTransform(scaleX: 1.0, y: 0.000001)
        }) { (_) in
            transitionContext.completeTransition(true)
        }
        
    }
}

